package com.wmd.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description:
 * @author: luyincheng
 */
public class TimeTool {
	public static String simpleDateFormat_1 = "yyyy-MM-dd HH:mm:ss";
	public static String simpleDateFormat_2 = "yyyyMMddHHmmss";
	public static String simpleDateFormat_3 = "yyyyMMdd";
	public static String simpleDateFormat_4 = "yyyy/MM/dd HH:mm:ss";
	public static String simpleDateFormat_5 = "yyyy/MM/dd HH:mm";
	public static String simpleDateFormat_6 = "MM月dd日";
	public static String simpleDateFormat_7 = "MM-dd";
	public static String simpleDateFormat_8 = "yyyy-MM-dd";
	public static String simpleDateFormat_9 = "yyyy-MM";
	public static String simpleDateFormat_10 = "HH:mm:ss";
	public static String simpleDateFormat_11 = "yyyyMMdd't'HHmmss'z'";

	public static String getTime(String simpleDateFormat) {
		Date d = new Date();
		// System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}

	public static String getTime(Date d, String simpleDateFormat) {
		// System.out.println(d);
		if(d==null){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}

	public static Date StringToDate(String time, String simpleDateFormat) {
		if(StringUtil.isEmpty(time)){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat);
		try {
			return sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int timeCompare(String date1, String date2) {
		SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormat_1);
		try {
			Date date_01 = sdf.parse(date1);
			Date date_02 = sdf.parse(date2);
			long result = date_01.getTime() - date_02.getTime();
			return (int) result / 60000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static int timeCompare(Date date1, Date date2) {
		long result = date1.getTime() - date2.getTime();
		return (int) result / 60000;
	}

	public static int timeCompareDayDetail(Date date1, Date date2) {
		long result = date1.getTime() - date2.getTime();
		return (int) (result / 60000 / 60 / 24);
	}

	public static int timeCompareDayIgnoreHours(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		int day1 = cal.get(Calendar.DAY_OF_YEAR);
		cal.setTime(date2);
		int day2 = cal.get(Calendar.DAY_OF_YEAR);
		return day1 - day2;
	}

	public static int timeGetYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	public static String[] timeGetYMD(Date date) {
		String[] data = new String[3];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		data[0] = year + "";
		if (month >= 10) {
			data[1] = month + "";
		} else {
			data[1] = "0" + month;
		}
		data[2] = day + "";
		return data;
	}

	public static int[] timeGetYMD_Int(Date date) {
		int[] data = new int[3];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		data[0] = year;
		data[1] = month;
		data[2] = day;
		return data;
	}
	public static int[] timeGetYMDHMS_Int(Date date) {
		int[] data = new int[6];
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour=cal.get(Calendar.HOUR_OF_DAY);
		int minute=cal.get(Calendar.MINUTE);
		int second=cal.get(Calendar.SECOND);
		data[0] = year;
		data[1] = month;
		data[2] = day;
		data[3]=hour;
		data[4]=minute;
		data[5]=second;
		return data;
	}
	public static String[] timeGetYMDHMS(Date date) {
		int[] intdata=timeGetYMDHMS_Int(date);
		String[] data=new String[6];
		data[0]=intdata[0]+"";
		data[1]=intToString(intdata[1],2);
		data[2]=intToString(intdata[2],2);
		data[3]=intToString(intdata[3],2);
		data[4]=intToString(intdata[4],2);
		data[5]=intToString(intdata[5],2);
		return data;
	}
	public static String intToString(Integer a,int b) {
		String data=(a+"").trim();
		int length=data.length();
		for(int i=b-length;i>0;i--) {
			data="0"+data;
		}
		return data;
	}
	public static String timeGetMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH) + 1;
		return month < 10 ? ("0" + month) : (month + "");
	}

	public static String timeGetDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return day + "";
	}

	public static String timeGetDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_YEAR);
		return day + "";
	}

	public static long timeCompareDetail(Date date1, Date date2) {
		long result = date1.getTime() - date2.getTime();
		return result;
	}

	public static Date timePlusYear(Date now, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}

	public static Date timePlusMonth(Date now, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}

	public static Date timePlusDay(Date now, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, day);
		return cal.getTime();
	}
	public static Date timePlusMinute(Date now, int minute) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.MINUTE, minute);
		return cal.getTime();
	}
	public static Date timePlusWeek(Date now, int week) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.WEEK_OF_MONTH, week);
		return cal.getTime();
	}

	/**
	 * 获取当年的第一天
	 * 
	 * @return
	 */
	public static Date getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		Date current = getYearFirst(currentYear);
		return current;
	}

	/**
	 * 获取某年第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	public static String getMaxDayOfMonth(Date date, String simpleDateFormat) {
		// 设置时间格式
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		// 获得实体类
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		// 设置最后一天
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		// 最后一天格式化
		String lastDay = format.format(ca.getTime());
		return lastDay;
	}

	public static String getMinDayOfMonth(Date date, String simpleDateFormat) {
		// 设置时间格式
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		// 获得实体类
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		// 设置最后一天
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
		// 最后一天格式化
		String lastDay = format.format(ca.getTime());
		return lastDay;
	}
	public static boolean isWeekend(Date date){
		//设置日期
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//判断周6，周日
		if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY  ||
				calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
			return true;
		}else {
			return false;
		}
	}
	/*
	 * @Test
	 * 
	 * @Ignore public void testTime() { try {
	 * System.out.println("aa:"+TimeTool.test2()); } catch (TimeOutException e) { //
	 * TODO Auto-generated catch block e.printStackTrace(); } }
	 * 
	 * @Test public void testFile() { File file =new
	 * File("xdkbArticle\\SearchResult\\baidu\\03\\20180109144516_.json "); File
	 * parent=file.getParentFile(); System.out.println(file.getAbsolutePath());
	 * System.out.println(parent.getAbsolutePath());
	 * System.out.println(file.getParent()); file.mkdirs(); }
	 */

}
